/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sunspotworld.mydatawrappers;

import org.sunspotworld.datawrappers.IDataWrapperFactory;
import org.sunspotworld.datawrappers.DataWrapper;
import java.io.IOException;

/**
 * A Factory for making TrackingPackets from radio messages
 * @author Douglas McClusky
 */
public class TrackingPacketFactory implements IDataWrapperFactory{
    private static TrackingPacketFactory factory = new TrackingPacketFactory();
    
    public static TrackingPacketFactory getInstance() {
        return factory;
    }
    
    /**
     * Creates a TrackingPacket from the data within rg
     * @param rg
     * @return
     * @throws java.io.IOException
     */
    public DataWrapper constructDataWrapper(byte[] b, int index) throws IOException {
        System.out.println("Type: " + b[index]);
        switch (b[index]) {
            case TrackingPacket.GPS_PACKET:
                System.out.println("GPS Packet");
                return new GPSPacket(b, index);
            case TrackingPacket.NEIGHBOR_PACKET:
                System.out.println("Neighbor Packet");
                return new NeighborPacket(b, index);
            case TrackingPacket.LIGHT_PACKET:
                System.out.println("Light Packet");
                return new LightPacket(b, index);
            case TrackingPacket.TEMP_PACKET:
                System.out.println("Temperature Packet");
                return new TempPacket(b, index);
            case TrackingPacket.POWER_PACKET:
                System.out.println("Power Packet");
                return new PowerPacket(b,index);
            default:
                System.out.println("Specified packet type: " + b[index] + " does not exist.");
                throw new IOException("Specified packet type: " + b[index] + " does not exist.");
        }
    }
    
    public String printHeader(TrackingPacket pkt) {
        switch (pkt.getType()) {
            case TrackingPacket.GPS_PACKET:
                return GPSPacket.HEADER;
            case TrackingPacket.NEIGHBOR_PACKET:
                return NeighborPacket.HEADER;
            case TrackingPacket.LIGHT_PACKET:
                return LightPacket.HEADER;
            case TrackingPacket.TEMP_PACKET:
                return TempPacket.HEADER;
            case TrackingPacket.POWER_PACKET:
                return PowerPacket.HEADER;
            default:
                return DataWrapper.printRecordHeader();
        }
    }

    public DataWrapper constructDataWrapper(byte[] b) throws IOException {
        return this.constructDataWrapper(b, 0);
    } //*/
}
